package com.smzd.projforge.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.core.actions.AbstractMeetingOwnedController;
import com.smzd.core.annotations.Access;
import com.smzd.core.business.AbstractService;
import com.smzd.core.utils.db.ORPage;
import com.smzd.framework.system.MeetingSessionHolder;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.projforge.business.CodeGeneratorService;
import com.smzd.projforge.business.ProjectService;
import com.smzd.projforge.domain.Project;

@Controller
@Access
@RequestMapping("/projects")
public class ProjectController extends AbstractMeetingOwnedController<Project> {
	@Autowired
	private ProjectService service;
	@Autowired
	private CodeGeneratorService cgService;

	@Override
	protected AbstractService<Project> getService() {
		return service;
	}

	@Override
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String toAddPage(Map<String, Object> model, Project elem,
			BindingResult result) throws Exception {
		return super.toAddPage(model, elem, result);
	}

	@Override
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String postAddData(Map<String, Object> model, Project elem,
			BindingResult result) throws Exception {
		return super.postAddData(model, elem, result);
	}

	@Override
	@RequestMapping(method = RequestMethod.GET)
	public String toListPage(Map<String, Object> model, Project elem,
			BindingResult result) throws Exception {
		return super.toListPage(model, elem, result);
	}

	@Override
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public String getListData(Map<String, Object> model, ORPage<Meeting, Project> paging,
			BindingResult result) throws Exception {
		return super.getListData(model, paging, result);
	}

	@Override
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String toEditPage(Map<String, Object> model, Project elem,
			BindingResult result) throws Exception {
		elem = service.getItemById(elem.getId());
		return super.toEditPage(model, elem, result);
	}

	@Override
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public String postEditData(Map<String, Object> model, Project elem,
			BindingResult result) throws Exception {
		return super.postEditData(model, elem, result);
	}

	@Override
	@RequestMapping(value = "/remove")
	@ResponseBody
	public String postRemoveData(Map<String, Object> model, Project elem,
			BindingResult result) throws Exception {
		return super.postRemoveData(model, elem, result);
	}

	@RequestMapping(value = "/generate/{id}")
	@Access
	public String generateCodes(Map<String, Object> model,
			@PathVariable("id") int id) throws Exception {
		Project project = cgService.generateCode(id);
		model.put("project", project);
		return this.getMyView("code");
	}

	@Override
	protected Meeting getOwner() {
		Meeting meeting = MeetingSessionHolder.getMeeting();
		return meeting;
	}

}
